#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Output a result as a short record.
"""

import utility
import urllib
import re
import doctype

# import local modules
try:
	from mod_python import apache
	recordfunctions = apache.import_module('recordfunctions')
except:
	import recordfunctions

def outshort(result, vars, req):
	# prepare output for title
	
	validurl = vars['validurl']
	boolquerystring = vars['boolquerystring']
	searchraw = vars['searchraw']
	pageresultnum = vars['pageresultnum']
	termslist = vars['termslist']

	# create url to send to tracker
	trackurl = utility.maketrackurl(validurl, result['recHash'], boolquerystring)

	# backup title - need clean copy for possible google search
	googletitle = result['mTitle']

	# verify title present
	if len(result['mTitle'].strip()) < 1:
		result['mTitle'] = '[No Title]'

	# shorten title if needed
	result['mTitle'] = utility.shorten(result['mTitle'], 85)

	# bold generic keywords and title keywords
	result['mTitle'] = utility.boldify(result['mTitle'], termslist['keywords'] + termslist['mTitle'])

	# if url is present
	if len(validurl) > 0:
		# add url click tracker script
		scriptrecord = """onclick='return recordClick("%s","%s");'""" % (result['recHash'], urllib.quote_plus(searchraw))

		# add link anchor tag
		result['mTitle'] = '<a %s href="%s">%s</a>' % (scriptrecord, validurl, result['mTitle'])

	# prepare output for creator

	# parse creators, provide quick author searches for each
	creators = recordfunctions.parsecreator(result['mCreator'])

	# shorten creators if needed
	result['mCreator'] = utility.shorten(result['mCreator'], 80)

	# bold search words
	creators = utility.boldify(creators, termslist['keywords'] + termslist['mCreator'])

	# link the creators to quick searches
	result['mCreator'] = recordfunctions.linkcreator(result['mCreator'], creators).strip()

	# prepare the output for subject

	# shorten subject if needed
	result['mSubject'] = utility.shorten(result['mSubject'], 80)

	# bold generic keywords and subject keywords
	result['mSubject'] = utility.boldify(result['mSubject'], termslist['keywords'] + termslist['mSubject'])

	# prepare output for description

	# shorten description if needed
	result['mDescription'] = utility.shorten(result['mDescription'], 400)

	# bold generic keywords and description keywords
	result['mDescription'] = utility.boldify(result['mDescription'], termslist['keywords'] + termslist['mDescription'])

	# output

	### TITLE
	req.write( result['mTitle'] )

	# doc type icon
	req.write( doctype.gettypeicon(result['mFormat']) )
	
	req.write("""<font size="-1">""")

	### ACTIONS
	data = (result['recHash'], pageresultnum, urllib.quote_plus(searchraw), result['recHash'], result['recHash'])
	req.write("""&nbsp; <a title="View Full Metadata" 
		onClick="sendExpand('%s','res%s','%s');">
		<span class="details">Expand</span></a>
		<![if gt IE 7]>&nbsp; <a
		onClick="addTab('%s'); rememberTab('%s'); sendRemember(); checkMove(0);">
		<span class="details">Remember</span></a><![endif]>
		<br>""" % data)

	### CREATOR & SUBJECT

	# verify length
	if len(result['mCreator']) > 2:
		# print creator
		req.write("""<i>%s</i>""" % result['mCreator'])

		# add dash if subject will be printed
		if len(result['mSubject']) > 2:
			# print dash
			req.write(""" &nbsp;-&nbsp; """)

		# end dash check

	# end creator

	# verify length
	if len(result['mSubject']) > 2:
		# print subject
		req.write("""<span class="grey">%s</span>""" % result['mSubject'])
		# end print subject
	# end subject

	# line break check
	if len(result['mCreator']) > 2 or len(result['mSubject']) > 2:
		# print a break
		req.write("""<br>""")

	# end break check

	### DESCRIPTION

	# out description
	if not utility.empty(result['mDescription']):
		# begin

		req.write( result['mDescription'] )
		req.write("""<br>""")

	# end		


	### URL & DATE

	# greenify it
	req.write("""<span class="greenify" """)

	# if there was no valid url found, allow for a google scholar search
	if validurl == '':
		# get the title and clean it up a bit if possible
		googletitle = utility.cleantitle(googletitle)

		# generate the google scholar link
		req.write("""><a
			href="http://scholar.google.com/scholar?q=%%22%s%%22"
			>Search Google Scholar</a>
			""" % urllib.quote_plus(googletitle) )

		# also normal google search
		req.write("""&nbsp;&nbsp;<a
			href="http://www.google.com/search?q=%%22%s%%22"
			>Search Google</a>
			""" % urllib.quote_plus(googletitle) )

	else:
		# if valid url is present, output it
		req.write(""">%s""" % validurl)

	# display datestamp only if present
	if not utility.empty(result['hDatestamp']):
		dateMan = re.search('(.+)(T.*)$', result['hDatestamp'])

		# shorten datestamp if needed
		if dateMan is not None:
			result['hDatestamp'] = dateMan.groups()[0]

		# print datestamp
		req.write(""" &nbsp;-&nbsp; %s"""  % result['hDatestamp'])

	req.write("""</span>""")

	req.write("""</font>""")

# eof

